/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package classcreator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Vector;

/**
 *
 * @author Stefano
 */
public class MethodHeader {
    private static double outputFormatVersion=1.0;
    
    public Visibility visibility=Visibility.publ;
    public String name;
    public Vector<Parameter> parameters=new Vector<Parameter>();
    public Type returnType;
    public boolean isStatic=false;
    
    public MethodHeader(Type rt, String n){
        name=n;
        returnType=rt;
    }
    
    public void addParameter(Parameter p){
        parameters.add(p);
    }
    
    @Override
    public String toString(){
        String s=visibility.toString()+" ";
        if(isStatic) s=s+"static ";
        s=s+returnType.toString()+" "+name+"(";
        boolean changed=false;
        for(Parameter p:parameters){
            changed=true;
            s=s+p.toString()+",";
        }
        if(changed) s=s.substring(0, s.length()-1);
        s=s+")";
        return s;
    }
    
    public static MethodHeader parse(BufferedReader br, TypeRepository tr) throws IOException{
                br.readLine();
                double version=Double.parseDouble(br.readLine());
                
                if(version<=1.0){
                    Visibility vis=Visibility.parse(br.readLine());
                    String name=br.readLine();
                    int n=Integer.parseInt(br.readLine());
                    Vector<Parameter> params=new Vector<Parameter>();
                    for(int i=0; i<n; i++){
                        params.add(Parameter.parse(br, tr));
                    }
                    Type t=Type.parse(br, tr);
                    boolean iss=Boolean.parseBoolean(br.readLine());
                    MethodHeader mh=new MethodHeader(t,name);
                    for(Parameter p:params) mh.addParameter(p);
                    mh.visibility=vis;
                    mh.isStatic=iss;
                    return mh;
                }
                //Actual version
                Visibility vis=Visibility.parse(br.readLine());
                String name=br.readLine();
                int n=Integer.parseInt(br.readLine());
                Vector<Parameter> params=new Vector<Parameter>();
                for(int i=0; i<n; i++){
                    params.add(Parameter.parse(br, tr));
                }
                Type t=Type.parse(br, tr);
                boolean iss=Boolean.parseBoolean(br.readLine());
                MethodHeader mh=new MethodHeader(t,name);
                for(Parameter p:params) mh.addParameter(p);
                mh.visibility=vis;
                mh.isStatic=iss;
                return mh;
    }
    
    public void permanentize(BufferedWriter br, TypeRepository tr) throws IOException{
        br.write("/***/ Method Header: "+name); br.newLine();
        br.write(String.valueOf(outputFormatVersion)); br.newLine();
                visibility.permanentize(br, tr); 
                br.write(name); br.newLine();
                br.write(String.valueOf(parameters.size())); br.newLine();
                for(Parameter p:parameters){
                    p.permanentize(br, tr); 
                }
                returnType.permanentize(br, tr); 
                br.write(String.valueOf(isStatic)); br.newLine();
    }
}
